package com.markyao.service.db.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.markyao.mapper.ConfigInfoMapper;
import com.markyao.mapper.HisConfigInfoMapper;
import com.markyao.service.db.AbstractConfigService;
import com.markyao.service.db.ConfigInfoService;
import com.markyao.service.db.HisConfigService;
import com.markyao.service.raft.BroadcastConfigInfoService;
import com.markyao.utils.DateParseUtils;
import com.markyao.vo.ConfigInfoVo;
import com.markyao.vo.PageVo;
import com.markyao.vo.QueryParams;
import com.markyao.vo.Result;
import com.markyao.vo.pojo.ConfigInfo;
import com.markyao.vo.pojo.HisConfigInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@Service
public class HisConfigServiceImpl extends AbstractConfigService implements HisConfigService {
    @Autowired
    HisConfigInfoMapper hisConfigInfoMapper;
    @Autowired
    ConfigInfoMapper configInfoMapper;
//    @Autowired
//    BroadcastConfigInfoService broadcastConfigInfoService;
    @Override
    public Result selectList(QueryParams queryParams) {
        List<HisConfigInfo>configInfoList=null;
        StringBuilder querySql=new StringBuilder("WHERE ");
        int cnt=0;
        if (StringUtils.hasText(queryParams.getDataId())){
            querySql.append("data_id =#{queryParam.dataId}");
            cnt++;
        }
        if(StringUtils.hasText(queryParams.getGroupId())){
            if (cnt==0){
                querySql.append("group_id = #{queryParam.groupId}");
            }else {
                querySql.append("AND group_id = #{queryParam.groupId}");
            }
            cnt++;
        }
        if (StringUtils.hasText(queryParams.getAppName())){
            if (cnt==0){
                querySql.append("app_name = #{queryParam.appName}");
            }else {
                querySql.append("AND app_name = #{queryParam.appName}");
            }
            cnt++;
        }
        PageHelper.startPage(queryParams.getCurrentPage(),queryParams.getPageSize());
        if (cnt==0){
            configInfoList=hisConfigInfoMapper.selectList();
        }else {
            configInfoList=hisConfigInfoMapper.selectListByQuery(queryParams,querySql.toString());
        }
        if (configInfoList==null){
            return Result.success("配置列表为空",null);
        }
        PageInfo pageInfo=new PageInfo(configInfoList);
        PageVo pageVo=new PageVo();
        pageVo.setTotal(pageInfo.getTotal());
        List<ConfigInfoVo> configInfoVos = copyList(configInfoList);
        pageVo.setList(configInfoVos);
        return Result.success(pageVo);
    }

    @Override
    public ConfigInfoVo selectOne(QueryParams queryParams) {
        if (queryParams.getId()!=null && queryParams.getId().length()>0){
            HisConfigInfo hisConfigInfo=hisConfigInfoMapper.selectOneById(queryParams.getId());
            return copy(hisConfigInfo);
        }
        return null;
    }

    /**
     * 回滚配置:
     * 1.查出目标配置信息
     * 2.保存该dataId的使用版本为历史版本(config_info表中)
     * 前两步可概括为跟目标配置对换
     * 3.通知刷新
     * @param id
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result rollback(String id) {
        ConfigInfoVo configInfoVo = interChange(id);
        boolean b = adviceRefresh(configInfoVo);
        if (!b){
            throw new RuntimeException("通知更新失败");
        }
//        boolean f=broadcastConfigInfoService.rollback(id);
        return Result.success("回滚成功",null);
    }
    /**
     * 版本表 与 配置表互换
     * @param hisId
     * @throws RuntimeException
     */
    private ConfigInfoVo interChange(String hisId) throws RuntimeException{
        HisConfigInfo hisConfigInfo = hisConfigInfoMapper.selectOneById(hisId);
        String dataId = hisConfigInfo.getDataId();
        ConfigInfo configInfo = configInfoMapper.selectByDataId(dataId);
        HisConfigInfo insertHisConfig = copy0(configInfo);
        insertHisConfig.setId(Long.valueOf(hisId));
        int updateHis = hisConfigInfoMapper.updateById(insertHisConfig);
        if (updateHis<=0){
            throw new RuntimeException("更新历史表失败!");
        }
        ConfigInfo insertConfig = copy0(hisConfigInfo);
        int updateConf=configInfoMapper.updateByDataId(insertConfig);
        if (updateConf<=0){
            throw new RuntimeException("更新配置失败!");
        }
        return abscopy(insertConfig);
    }


    @Override
    public Result deletes(String[] ids) {
        int delete=hisConfigInfoMapper.deleteBatch(ids);
        if (delete>0){
            return Result.success("删除历史版本信息成功",null);
        }
//        boolean f=broadcastConfigInfoService.deleteHisConfig(ids);
        return Result.failed(500,"删除历史版本信息失败");
    }







}
